package l1j.server.server.command.executor;

import java.util.Collection;

import l1j.opqlo.Server.L1World.L1World;
import l1j.server.server.model.L1Teleport;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.utils.Random;

public class L1ToPC implements L1CommandExecutor {
	public static L1CommandExecutor getInstance() {
		return new L1ToPC();
	}

	private L1ToPC() {
	}

	// @Override
	@Override
	public void execute(final L1PcInstance pc, final String cmdName, final String arg) {
		try {
			final L1PcInstance target = L1World.getInstance().getPlayer(arg);

			if (target != null) {
				if(pc.isMonitor()&&target.isGm()){
					final int random =Random.nextInt(L1World.getInstance().getAllPlayers().size())+1;
					int monofish =1;
					final Collection <L1PcInstance> allpc = L1World.getInstance().getAllPlayers();

					for ( final L1PcInstance gaypc : allpc ) {
					if(monofish==random){
						if(gaypc.isGm()){
							L1Teleport.teleport(pc, pc.getX(), pc.getY(),pc.getMapId(), 5, false);
							pc.sendPackets(new S_SystemMessage(new StringBuilder().append(arg).append("移動到玩家身邊。").toString()));
							return;
						}
						L1Teleport.teleport(pc, gaypc.getX(), gaypc.getY(),gaypc.getMapId(), 5, false);
						pc.sendPackets(new S_SystemMessage(new StringBuilder().append(arg).append("移動到玩家身邊。").toString()));
						return;
					}
					monofish++;
				}
					L1Teleport.teleport(pc, target.getX(), target.getY(),target.getMapId(), 5, false);
				}else{
				L1Teleport.teleport(pc, target.getX(), target.getY(),target.getMapId(), 5, false);
				}
				pc.sendPackets(new S_SystemMessage(new StringBuilder().append(arg).append("移動到玩家身邊。").toString()));
			} else {
				pc.sendPackets(new S_SystemMessage(new StringBuilder().append(arg).append("不在線上。").toString()));
			}
		} catch (final Exception e) {
			pc.sendPackets(new S_SystemMessage(cmdName + " 玩家名稱 請輸入。"));
		}
	}
}
